package org.lebutch.sw.activity;

import org.lebutch.sw.R;
import org.lebutch.sw.SevenWondersApp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	public static final int MENU_HELP = 1;
	public static final int MENU_OPTIONS = 2;
	public static final int MENU_ABOUT = 3;

	private SevenWondersApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		app = (SevenWondersApp) getApplication();

		Spinner spinner = (Spinner) findViewById(R.id.spinnerPlayersCount);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.players_array,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);

		int firstItem = Integer.parseInt(adapter.getItem(0).toString());

		/*
		 * Log.d(Constants.LOG_TAG, "Preparing spinner playersCount: " +
		 * (app.getCurrentPlayersCount() - firstItem) + " from base: " +
		 * firstItem);
		 */
		spinner.setSelection(app.getCurrentPlayersCount() - firstItem);

		Button button = (Button) findViewById(R.id.randomizeButton);
		button.setOnClickListener(this);
	}

	/*
	 * @Override protected void onResume() { super.onResume();
	 * app.readPreferences(); }
	 */

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getOrder()) {
		case MENU_HELP:
			onHelp();
			return true;
		case MENU_OPTIONS:
			onOptions();
			return true;
		case MENU_ABOUT:
			onAbout();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	public void onClick(View view) {
		Spinner spinner = (Spinner) findViewById(R.id.spinnerPlayersCount);
		int pc = Integer.parseInt((String) spinner.getSelectedItem());
		app.setCurrentPlayersCount(pc);

		Intent resultActivity = new Intent(MainActivity.this,
				ResultActivity.class);
		startActivity(resultActivity);
	}

	private void onAbout() {
		AlertDialog.Builder builder;
		LayoutInflater inflater = (LayoutInflater) getApplicationContext()
				.getSystemService(LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.about,
				(ViewGroup) findViewById(R.id.layout_root));

		/*
		 * ImageView image = (ImageView) layout.findViewById(R.id.image);
		 * image.setImageResource(R.drawable.sw_aqueduc);
		 */
		TextView tv = (TextView) layout.findViewById(R.id.text);
		String text = String.format(
				getResources().getString(R.string.about_text),
				app.getApplicationVersion());
		tv.setText(text);

		builder = new AlertDialog.Builder(this)
				.setTitle(R.string.about_title)
				.setIcon(android.R.drawable.ic_dialog_info)
				.setView(layout)
				.setPositiveButton(getString(android.R.string.ok),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
							}
						});
		builder.create().show();
	}

	private void onHelp() {
		startActivity(new Intent(this, HelpActivity.class));
	}

	private void onOptions() {
		startActivity(new Intent(this, EditOptionsActivity.class));
	}
}